package com.qidian.k12.service.impl;

import com.qidian.k12.dto.PageInfo;
import com.qidian.k12.pojo.Student;
import com.qidian.k12.mapper.StudentMapper;
import com.qidian.k12.service.StudentService;
import com.qidian.k12.util.K12Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * (Student)表服务实现类
 *
 * @author makejava
 * @since 2021-10-28 17:57:25
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Override
    public K12Result queryByNameAndNo(String no, String name) {
        return K12Result.ok(studentMapper.queryByNameAndNo(no,name));
    }


    @Override
    public PageInfo<Student> query(Map map) {
        System.out.println(map);
        PageInfo<Student> pageInfo=new PageInfo<>();

        int page = new Integer(map.get("page").toString());

        int row = new Integer(map.get("row").toString());
        pageInfo.setPage(page);
        pageInfo.setRow(row);

        int total=studentMapper.total(map);

        pageInfo.setTotal(total);

        int maxPageNum=(int)(Math.ceil(total*1.0/row));
        pageInfo.setMaxPageNun(maxPageNum);
        //计算分页栏位置
        int pageStart=1;
        int pageEnd=10;
        if(page-4>1){
            pageStart=page-4;
        }
        if (page+5>maxPageNum){
            pageEnd=maxPageNum;
        }

        pageInfo.setPageStart(pageStart);
        pageInfo.setPageEnd(pageEnd);
        //数据 放在 pageInfo.data
        pageInfo.setData(studentMapper.query(map));
        return pageInfo;
    }



    /**
     * 通过ID查询单条数据
     *
     * @param studentNo 主键
     * @return 实例对象
     */
    @Override
    public Student queryById(String studentNo) {
        return this.studentMapper.queryById(studentNo);
    }


    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public K12Result insert(Student student) {
        K12Result result=K12Result.error();
        if (studentMapper.queryByTel(student.getStudentTel())!=null){
            return  K12Result.dataEXists();
        }
        Date date=new Date();
        student.setCreateTime(date);
        student.setModifyTime(date);
        student.setStudentStatus(1);
        if (studentMapper.insert(student)==1){
            result=K12Result.ok();
        }

        return result;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public K12Result update(Student student) {
        K12Result result=K12Result.error();
        student.setModifyTime(new Date());
        if (studentMapper.update(student)==1){
            result=K12Result.ok(this.queryById(student.getStudentNo()));
        }
        return result;
    }

    /**
     * 通过主键删除数据
     *
     * @param studentNo 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String studentNo) {
        return this.studentMapper.deleteById(studentNo) > 0;
    }
}
